package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.ContractBean;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.ContractQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;


/**
 * @author 供水合同修改 cd
 */
public class WaterSupplyContractUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class);
            ContractBean updatebean = JsonUtils.objectToPojo(req.getData(), ContractBean.class);
            ContractQueryBean queryBean = new ContractQueryBean();
            ContractBean before = mapper.queryContractBean(updatebean.getUserNo());
            //比较变更数据,插入到变更历史表
            List<UserChangeItemBean> list = compare(factory, before, updatebean, userInfo);
            if (0 == list.size()) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "用户信息无变更事项", null);
            }
            ChangeUserMapper changemapper = factory.getMapper(ChangeUserMapper.class);

            changemapper.userChangeItemAdd(list, 1);

            //更新信息到用户表和客户表
            mapper.updateContractBeanToUser(updatebean);
            mapper.updateContractBeanToCtm(updatebean);
            @SuppressWarnings("rawtypes")
            MessageBean<HashMap> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", HashMap.class);
            info.setData(null);
            return info;
        } catch (Exception e) {
            logger.error("供水合同信息修改失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "供水合同信息修改失败", null);
        }
    }

    private List<UserChangeItemBean> compare(SessionFactory factory, ContractBean before, ContractBean updatebean, UserInfoBean userInfo) throws IllegalAccessException, IntrospectionException, InvocationTargetException {

        Map<String, String> fields = new HashMap<>();
        Map<String, String> userField = Tools.getFiledName(factory, "user_info");
        Map<String, String> ctmField = Tools.getFiledName(factory, "user_ctm");
        fields.putAll(userField);
        fields.putAll(ctmField);
        Map<String, List<Object>> changes = Tools.compareFields(before, updatebean, fields);


        List<UserChangeItemBean> list = new ArrayList<UserChangeItemBean>();
        // 比较对象变更字段值

        if (changes != null && changes.size() != 0) {
            // 取出所有的变更项的主键
            Set<String> set = changes.keySet(); // 取出所有的key值
            for (String key : set) {
                if (!fields.containsKey(key)) {
                    continue;
                }
                UserChangeItemBean bean = new UserChangeItemBean();
                bean.setCreateInfo(userInfo);
//                    bean.setReceiptId(changeNos.get("receiptId"));
//                    bean.setReceiptType(changeNos.get("receiptType"));
//                    bean.setUserNo(changeNos.get("userNo"));
                bean.setUserNo(before.getUserNo());
//                    bean.setCtmNo(changeNos.get("ctmNo"));
//                    bean.setAccountNo(changeNos.get("accountNo"));
//                    bean.setBillNo(changeNos.get("billNo"));
                bean.setChangeItem(fields.get(key));
                bean.setOldItemValue(changes.get(key).get(0).toString());
                bean.setNewItemValue(changes.get(key).get(1).toString());
                list.add(bean);
            }
        }

        //比较附件是否更新，如果更新，插入到变更历史
        if (updatebean.getAccessoryList() != null && updatebean.getAccessoryList().size() != 0) {
            List<String> accessoryList = new ArrayList<String>(new TreeSet<String>(updatebean.getAccessoryList()));
            UserChangeItemBean bean = new UserChangeItemBean();
            bean.setCreateInfo(userInfo);
            bean.setUserNo(before.getUserNo());
            String accessoryMsg = "";
            for (String s : accessoryList) {
                accessoryMsg = accessoryMsg + s;
            }
            bean.setChangeItem("附件" + accessoryMsg + "已更新");
            list.add(bean);
        }
        return list;

    }
}
